package net.zjitc.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import net.zjitc.common.BaseContext;
import net.zjitc.common.R;
import net.zjitc.entity.AddressBook;
import net.zjitc.service.AddressBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/addressBook")
@Slf4j
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;
    @PostMapping
    public R<String> save(@RequestBody AddressBook addressBook){
        log.info("保存地址:{}",addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return R.success("保存成功");
    }
    @GetMapping("/list")
    public R<List<AddressBook>> getList(AddressBook addressBook){
        log.info("addressBook:{}",addressBook);
        addressBook.setUserId(BaseContext.getCurrentId());
        LambdaQueryWrapper<AddressBook> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(addressBook.getUserId()!=null,AddressBook::getUserId,addressBook.getUserId());
        wrapper.orderByDesc(AddressBook::getUpdateTime);
        return R.success(addressBookService.list(wrapper));
    }
    @PutMapping("/default")
    public R<String> setDefault(@RequestBody AddressBook addressBook){
        log.info("addressBook{}",addressBook);
        LambdaUpdateWrapper<AddressBook> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(AddressBook::getUserId,BaseContext.getCurrentId());
        wrapper.set(AddressBook::getIsDefault,0);
        addressBookService.update(wrapper);
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("设置默认地址成功");
    }
    @GetMapping("/default")
    public R<AddressBook> getDefault(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getIsDefault,1);
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId,currentId);
        AddressBook one = addressBookService.getOne(addressBookLambdaQueryWrapper);
        return R.success(one);
    }
    @GetMapping("/{id}")
    public R<AddressBook> getById(@PathVariable Long id){
        log.info("根据id查询"+id);
        AddressBook addressBookServiceById = addressBookService.getById(id);
        return R.success(addressBookServiceById);
    }
    @PutMapping
    public R<String> updateById(@RequestBody AddressBook addressBook){
        log.info("修改"+addressBook);
        addressBookService.updateById(addressBook);
        return R.success("修改成功");
    }
    @DeleteMapping
    public R<String> deleteById(Long ids){
        log.info("删除的用户的id:"+ids);
        addressBookService.removeById(ids);
        return R.success("删除成功");
    }
}
